package com.supermap.shiro.common;

import com.supermap.dubbo.api.user.entity.BindUser;
import com.supermap.dubbo.api.user.entity.User;
import com.supermap.shiro.auth.entity.AppUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * @author xiaolu
 * @date 2018/7/3 上午11:48
 */
public class AuthFactory {
    public static AppUser getLoginUser(boolean showPassword) {
        Subject subject = SecurityUtils.getSubject();
        AppUser user = (AppUser) subject.getPrincipal();
        try {
            user = user == null ? null : user.clone();
        } catch (CloneNotSupportedException e) {
            user = null;
        }
        if (user != null && !showPassword) {
            user.setPassword(null);
            user.setRealPassword(null);
        }
        return user;
    }

    public static Session getSession() {
        Subject subject = SecurityUtils.getSubject();
        return subject.getSession();
    }

    public static AppUser updateUser(User tmp) {
        Subject subject = SecurityUtils.getSubject();
        AppUser user = (AppUser) subject.getPrincipal();
        user.updateUser(tmp);
        return user;
    }
    public static AppUser updateBindUser(BindUser bindUser){

        Subject subject =  SecurityUtils.getSubject();
        AppUser user = (AppUser) subject.getPrincipal();
        user.updateBindUser(bindUser);
        return user;
    }
}
